<?php

namespace app\api\controller;

use app\common\controller\Api;
use addons\shopro\model\Store as StoreModel;
use app\admin\model\ShoproStoreProject;
use app\admin\model\ShoproStoreWaiter;
use app\common\model\ShoproStoreNews;

/**
 * 店铺接口
 */
class Store extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $params = $this->request->param();

        $field = 'id,name,desc,images,realname,phone,province_name,city_name,area_name,address,longitude,latitude,store,openhours,openweeks';
        $stores = StoreModel::getStoreList($params, $field);

        $this->success('请求成功', $stores);
    }

    public function news()
    {
        $params = $this->request->param();

        $field = 'id,shopro_store_id,title,subtitle,images,createtime,updatetime';
        $news = ShoproStoreNews::getNewsList($params, $field);

        $this->success('请求成功', $news);
    }

    public function news_detail($id = null)
    {
        if (!$id) {
            $id = $this->request->get('id');
        }
        if (!$id) {
            $this->error('请求异常，请刷新重试');
        }

        try {
            $detail = ShoproStoreNews::getNewsDetail($id);
        } catch(\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('请求成功', $detail);
    }

    /**
     * 项目列表
     *
     * @return json
     */
    public function store_project_list($store_id = null)
    {
        if (!$store_id) {
            $store_id = $this->request->get('store_id');
        }
        if (!$store_id) {
            $this->error('请求异常，请刷新重试');
        }
        $stores = ShoproStoreProject::where(['shopro_store_id' => $store_id])->order('id desc')->paginate(10);

        $this->success('请求成功', $stores);
    }

    /**
     * 项目技师列表
     *
     * @return json
     */
    // public function store_waiter_list($project_id = null)
    // {
    //     if (!$project_id) {
    //         $project_id = $this->request->get('project_id');
    //     }
    //     if (!$project_id) {
    //         $this->error('请求异常，请刷新重试');
    //     }
    //     $stores = ShoproStoreWaiter::where(['shopro_store_project_id' => $project_id])->order('id desc')->paginate(10);

    //     $this->success('请求成功', $stores);
    // }

    /**
     * 项目技师列表
     *
     * @return json
     */
    public function store_waiter_list($store_id = null)
    {
        if (!$store_id) {
            $store_id = $this->request->get('store_id');
        }
        if (!$store_id) {
            $this->error('请选择预约店铺');
        }
        $stores = ShoproStoreWaiter::where(['shopro_store_id' => $store_id])->order('id desc')->paginate(10);
        $this->success('请求成功', $stores);
    }
}
